home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3399 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  107 lines

  1. Path: bunyip.cc.uq.oz.au!peg!tmccoy
  2. From: tmccoy@peg.apc.org
  3. Newsgroups: comp.lang.c
  4. Date: 29 Jan 96 01:38 GMT+1000
  5. Subject: Nested Structures in C - A Question
  6. Message-ID: <36400002@peg>
  7. Sender: Notesfile to Usenet Gateway <notes@peg.apc.org>
  8.  
  9. A Question on Nested Structures in C
  10. ------------------------------------
  11.  
  12. I've recently been doing some work on the design of the C
  13. programming language and there seems to be an inconsistency
  14. in the way nested structures are defined.
  15.  
  16. If I want to declare a simple structure called "outer" I
  17. can do the following:
  18.  
  19. struct outer {
  20.     int var1;
  21.     int var2;
  22.     int var3;
  23. };
  24.  
  25. Apparently the compiler will NOT allocate any storage when
  26. I do this because, according to Kernighan and Ritchie (2nd
  27. Ed), "a structure declaration that is not followed by a
  28. list of variables reserves no storage; it merely describes
  29. a template or the shape of a structure" (Page 128).
  30.  
  31. This is all well and good.  And when I want to define a new
  32. structure *within* my old one, I should be able to do:
  33.  
  34. struct outer {
  35.     int var1;
  36.     int var2;
  37.     int var3;
  38.     struct inner {
  39.         int nested1;
  40.         int nested2;
  41.     };
  42. };
  43.  
  44. and I should be able to define an instance of my structure
  45. by doing:
  46.  
  47. struct outer instance;
  48.  
  49. and refer to the first member of my inner structure by doing:
  50.  
  51. instance.inner.nested1
  52.  
  53. Yet, C won't let me do this!!  It insists that I define my
  54. nested structure as follows:
  55.  
  56. struct outer {
  57.     int var1;
  58.     int var2;
  59.     int var3;
  60.     struct inner {
  61.         int nested1;
  62.         int nested2;
  63.     } DUMMY;
  64. };
  65.  
  66. and after doing
  67.  
  68. struct outer instance;
  69.  
  70. I must refer to the first member of my inner structure by
  71. doing:
  72.  
  73. instance.DUMMY.nested1
  74.  
  75. i.e. I must access the inner members via a variable (called
  76. "DUMMY") instead of being able to use my structure tag
  77. (i.e. "inner").  In fact, the *only way* I can access the
  78. inner members is by defining the DUMMY variable within the
  79. structure template of "outer".
  80.  
  81. I am just wondering whether anybody else has found this to
  82. be strange and if anybody knows why the nested structure
  83. feature has been designed in this way.
  84.  
  85. I'm also curious about how compilers would implement this
  86. feature.
  87.  
  88. With a nested structure, as with a simple structure, I
  89. *should* be able to define a pure structure template in
  90. which no storage is allocated.  Yet, in the definition I am
  91. forced to use above, strictly speaking the compiler should
  92. allocate storage for "DUMMY" even though the enclosing
  93. structure (i.e. "outer") is only a template.
  94.  
  95. I find this very inconsistent and it is a feature that bugs
  96. me about an otherwise elegant language.
  97.  
  98. Does it bother anybody else?  Or am I just looking at it
  99. all the wrong way?
  100.  
  101. Any comments, via conference or e-mail, would be
  102. appreciated.
  103.  
  104. Cheers from Canberra, Australia,
  105.  
  106. Tom
  107.